'use strict'
const path = require('path')
console.log('R环境地址', process.env.VUE_APP_REDIT_LOGIN_URL, process.env.VUE_APP_R_USERNAME, process.env.VUE_APP_R_PASSWORD)
console.log('Python环境地址', process.env.VUE_APP_PYTHONEDIT_URL)

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = 'Strategic Screen'

// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following method:
// port = 9527 npm run dev OR npm run dev --port = 9527
// const port = process.env.port || process.env.npm_config_port || 9537 // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: process.env.VUE_APP_VUE_PUBLIC_PATH,
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: false, // process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  // host: '0.0.0.0',
  // disableHostCheck: true,
  devServer: {
    open: false, //编译完是否自动打开网页
    host: process.env.VUE_APP_HOST, //指定使用地址，默认localhost，0.0.0.0代表可以被外界访问
    port: process.env.VUE_APP_PORT, // web网页端口号
    https: false, //编译失败时刷新页面
    hot: true, //开启热加载
    hotOnly: false,
    //设置代理
    proxy: {
      '/hermes': {
        target: process.env.VUE_APP_API_URL,
        _proxyTimeout: 10 * 60 * 1000,
        get proxyTimeout() {
          return this._proxyTimeout
        },
        set proxyTimeout(value) {
          this._proxyTimeout = value
        },
        timeout: 10 * 60 * 1000,
        changeOrigin: true, // 跨域
        ws: true,
        secure: false, // https请求时候使用true
        // 请求地址重写  http://front-end/api/login ⇒ http://api-url/login
        // pathRewrite: {
        //   '^/hermes': '/',
        // }
      },
    }
  },
  transpileDependencies: [
    'webpack-dev-server/client',
    'element-ui',
    process.env.NODE_ENV === 'development' ? ['*'] : []
  ],
  configureWebpack: {
    name: name,
    devtool: 'source-map',
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  configureWebpack: config => {
    // 引入@babel/polyfill'模块
    config.entry.app = ['@babel/polyfill', '@/main.js']
  },
  css: {
    loaderOptions: {
      postcss: {
        // ie兼容grid布局，自动添加前缀
        plugins: [
          require('autoprefixer')({
            grid: 'autoplace'
          })
        ]
      }
    }
  },
  chainWebpack(config) {
    config.plugin('preload').tap(() => [{
      rel: 'preload',
      // to ignore runtime.js
      // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
      fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
      include: 'initial'
    }])

    // config.module
    //   .rule('vue')
    //   .use('vue-loader')
    //   .loader('vue-loader')
    //   .tap(options => {
    //     options.compilerOptions.directives = {
    //       html(node, directiveMeta) {
    //         (node.props || (node.props = [])).push({
    //           name: 'innerHTML',
    //           value: `xss(_s(${directiveMeta.value}))`
    //         })
    //       }
    //     }
    //     return options
    //   })

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development', config =>
        config.devtool('eval-source-map')
      )

    config.when(process.env.NODE_ENV !== 'development', config => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [{
          // `runtime` must same as runtimeChunk name. default is `runtime`
          inline: /runtime\..*\.js$/
        }])
        .end()
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' // only package third parties that are initially dependent
          },
          elementUI: {
            name: 'chunk-elementUI', // split elementUI into a single package
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
      config.optimization.runtimeChunk('single')
    })
  }
}